/**
 *加密处理
 */
import CryptoJS from 'crypto-js'
export const encryption = params => {
  let {data, type, param, key} = params;
  const result = JSON.parse(JSON.stringify(data));
  if (type === "Base64") {
    param.forEach(ele => {
      result[ele] = btoa(result[ele]);
    });
  } else {
    param.forEach(ele => {
      var data = result[ele];
      key = CryptoJS.enc.Latin1.parse(key);
      var iv = key;
      // 加密
      var encrypted = CryptoJS.AES.encrypt(data, key, {
        iv: iv,
        mode: CryptoJS.mode.CFB,
        padding: CryptoJS.pad.NoPadding
      });
      result[ele] = encrypted.toString();
    });
  }
  return result;
}


// 根据用户名登录
// LoginByUsername({commit}, userInfo) {
//   const user = encryption({
//     data: userInfo,
//     key: 'sirengine1234567',
//     param: ['password']
//   })
//   return new Promise((resolve, reject) => {
//     loginByUsername(user.username, user.password, user.code, user.randomStr).then(response => {
//       const data = response.data
//       commit('SET_ACCESS_TOKEN', data.access_token)
//       commit('SET_REFRESH_TOKEN', data.refresh_token)
//       commit('CLEAR_LOCK')
//       resolve()
//     }).catch(error => {
//       reject(error)
//     })
//   })
// }


// export const loginByUsername = (username, password, code, randomStr) => {
//   const grant_type = 'password'
//   let dataObj = qs.stringify({'username': username, 'password': password})

//   return request({
//     url: '/auth/oauth/token',
//     headers: {
//       isToken: false,
//       Authorization: 'Basic cGlnOnBpZw=='
//     },
//     method: 'post',
//     params: {randomStr, code, grant_type, scope},
//     data: dataObj
//   })
// }
